package com.guhappy.model.contest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.guhappy.util.HibernateListener;

public class ContestStockHome {
	private static final Logger log = Logger.getLogger(ContestStockHome.class);

	private static final SessionFactory sessionFactory = getSessionFactory();

	protected static SessionFactory getSessionFactory() {
		SessionFactory sessionFactory = 
	         (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);
		return sessionFactory;
	}

	public static void persist(ContestStock transientInstance) {
		log.debug("persisting ContestStock instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public static void attachDirty(ContestStock instance) {
		log.debug("attaching dirty ContestStock instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(ContestStock instance) {
		log.debug("attaching clean ContestStock instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(ContestStock persistentInstance) {
		log.debug("deleting ContestStock instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static ContestStock merge(ContestStock detachedInstance) {
		log.debug("merging ContestStock instance");
		try {
			ContestStock result = (ContestStock) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static ContestStock findById(java.lang.String id) {
		log.debug("getting ContestStock instance with id: " + id);
		Session session=null;
		ContestStock contestst=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.contest.ContestStock c where c.id = :id");
			q.setString("id", id);
			contestst=(ContestStock) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return contestst;
	}
}
